module.exports = function ($scope, $rootScope, $state, $stateParams, caseBarService,$timeout, $cookies,newCase,caseBarDetailBarService) {
  $rootScope.showCaseDiv = false;
  $rootScope.closeModal();
  // $scope.searchTb = 1;
    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate() + 1);

    var namelist = [];

    /*需要引入的html*/
    $scope.userShowTemplateUrl="partials/utolity/bounced.html";

    $scope.isall = 'Created ';
    //按钮
    $scope.mNoneresultIf123=false;

    /*默认为处理中的案件*/
    $scope.pageSize = 15;
    $scope.ctId = "";
    $scope.ccfId = "";
    //新建自然人
    $scope.zname = '';
    $scope.sex = '';
    $scope.phone = '';
    $scope.birthday = '';
    $scope.nationality = '';
    $scope.profession = '';
    $scope.workplace = '';
    $scope.address = '';
    $scope.status = '';
    $scope.remark = '';
    //对方当事人
    $scope.caseId = 1;
    $scope.caseType = 3;
    $scope.caseClientType = 1;
    $scope.adversaryClientName = '';
    //案由
    $scope.typeId = '';
    $scope.parentId = 0;
    //当事人
    $scope.searchPeoInput = '';
    $scope.CaseFaRenModelInput = '';
    //当事人添加
    $scope.caseZiRanRenModel = '';
    //法人添加
    $scope.caseFrenRenModel = '';
    //查询律师成员
   // $scope.keywordID = '';
    //处理中、归档案件类型
    $scope.type = '';

//用户名
  var loginName = kbt.decrypt($cookies.get('cookieUserName'));

    //当前用户的名字
    $scope.mSponsor=$cookies.get('userPortraitName');

    var isUpdated = $cookies.get('cookieIsUpdated');

    //用于替换的名字
    $scope.cacaName= $scope.mSponsor;

    //新建案件相关的参数
    $scope.mNewCase = {
        //案件类别ID
        casetypeid: "",
        //案件类型ID
        CaseClassificationId: "",
        //审理程序ID
        casebusiness: "",
        //案由ID
        casecauseid: "",
        //原告、被告
        UserType: 1,
        //对方当事人类型
        strcaseType: 3,
        //审理程序名称
        ProcedureName: "",
        //弹出框时对应的律师类别
        caselvshitype: "",
        /**律师成员的判断条件*/
        lawyersflost: true,
        /**用户信息*/
        DataUser:{},
        /**律师的框默认不显示*/
        mSolicitorShow:false,
        //**协助律师数组*/
        assistance:new Array()
    };

    //当事人数组和对方当事人数组
    $scope.mMatterPerson = {
        //委托人数组
        Consignor: new Array(),
        //对方当事人数组
        adversaryClients: new Array()
    };

    /**律师成员的集合*/
    $scope.lawyers = new Array();

    //保存的时候设置种类
    $scope.mSortShow = {
        /* 案件类别*/
        m1: false,
        /*案件类型*/
        m2: false,
        /*案由*/
        m3: false,
        /*   当事人*/
        m4: false,
        /* 对方当事人*/
        m5: false,
        /* 律师成员*/
        m6: false
    };

    //律师列表需要的初始变量
    $scope.mNoneresultIf=false;

    //案由
    //面包屑里边的名字数组
    $scope.nameList = [];
    $scope.CaseAnyouModelName = '';//案由的名字


    /*当前用户ID*/
    $scope.biglawuserid = kbt.decrypt($cookies.get('cookieUserId'));
    //对方当事人列表
    //$scope.biglawuserid=$stateParams.s_id;
    //自然人列表
    $scope.name = '';
    //法人列表
    $scope.companyname = '';

    //律师成员
    $scope.pageNum = '0';
    $scope.pageSize = '10';
    $scope.pageNow = 1;




  //   /**
  //    * 初始化
  //    *
  //    */function initFref (){
  //     if($stateParams.from==null||$stateParams.from==''||$stateParams.from==undefined){
  //       $state.go('caseBar', {from: 1});
  //     }
  //       }
  // initFref();
    $scope.init = function () {
      //设定归档案件判断条件二
      $rootScope.typeIf=1;

        //如果参数类型为7，则跳转到案件列表已归档
        if ($stateParams.from == 7) {
            $scope.caseClassifyList($stateParams.from);
            $scope.type = $stateParams.from;
        }
        //如果参数类型为workbar，则跳转到案件列表弹出创建案件
        else if ($stateParams.from == 'workbar') {
            /*案件列表查询*/
           $scope.selectInfo(1, $scope.biglawuserid, "1", "10");
            /*新增案件*/
            $scope.turnCaseBar();
        }
        else {
          /*案件列表查询*/
          if ($stateParams.from != undefined && $stateParams.from == 7) {
            $scope.selectInfo(7, $scope.biglawuserid, "1", "10");
          } else {
            $scope.selectInfo(1, $scope.biglawuserid, "1", "10");
          }
        }

        /*用于固定导航条*/
        sessionStorage.navNum = 0;


    };

    /*从工作台点击加号后，跳转案件板弹出新建案件框方法*/
    $scope.turnCaseBar = function () {
        // $scope.newXin(s_id);
        $scope.newXin();
       // $("#myModal2").modal("show");
    };

    /*页面之间的tab切换*/
    /*处理中的案件*/
    $("#casebar-id-l").click(function () {
        $scope.caseClassifyList(1);
        $scope.type = 1;
        $("#casebar-id-l").addClass("casebar-active");
        $("#casebar-id-r").removeClass("casebar-active");
    });
    /*已归档的案件*/
    $("#casebar-id-r").click(function () {
        $scope.caseClassifyList(7);
        $scope.type = 7;
        $("#casebar-id-r").addClass("casebar-active");
        $("#casebar-id-l").removeClass("casebar-active");
    });

     $scope.caseBar_kong = false;
    /**
     * 根据类型切换案件列表（已归档，处理中）
     * @param type 1：处理中；2：已归档
     */
    $scope.caseClassifyList = function (type) {
      if (type == 7) {
        $(".casebar-center-l").css({color: "grey"});
        $(".casebar-center-r").css({color: "#53C1AF"});
        $(".casebar-img-l").attr('src', "img/case-cl-unselected.png");
        $(".casebar-img-r").attr('src', "img/case-gd-selected.png");
        $("#casebar-id-r").addClass("casebar-active");
        $("#casebar-id-l").removeClass("casebar-active");
        $(".case-bg").hide();
        $scope.isall = 'Archived ';
        // $scope.selectInfo(s_id, $scope.isall, 1, $scope.pageSize);
        $scope.selectInfo(type, $scope.biglawuserid, "1", "10");
        /*案件列表查询*/
      } else {
        $(".casebar-center-l").css({color: "#53C1AF"});
        $(".casebar-center-r").css({color: "grey"});
        $(".casebar-img-l").attr('src', "img/case-cl-selected.png");
        $(".casebar-img-r").attr('src', "img/case-gd-unselected.png");
        $(".case-bg").show();
        $scope.isall = 'Created ';
        //  $scope.selectInfo(s_id, $scope.isall, 1, $scope.pageSize);
        $scope.selectInfo(type, $scope.biglawuserid, "1", "10");
        /*案件列表查询*/
      }
    }

    /*案件列表查询*/
    $scope.selectInfo = function (status, creatorid, pageNum, pageSize) {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        caseBarService.selectInfo(status, creatorid, pageNum, pageSize).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if (data.code == 1) {
                if(data.data.list.length==0){
                  $scope.caseBar_kong = true;
                $('.caseBar_kong').css('display','block');
              }else {
                  $scope.caseBar_kong = false;
                  $('.caseBar_kong').css('display','none');
                }
                /*设置其他页码颜色*/
                $('.select').css('color', '#666');
                $(".pagCenter").show();
                $('.caseBar_kong').hide();
                $('.caseBar_list').show();
                $('.select').css('color', '#666');
                  // $scope.newXin_1Date = data.data;
                //根据字段设置图片
                setImg(data.data);

                $scope.totalPage = data.data.pages;//总页数
                $scope.totalCount = data.data.total;//总条数
                $scope.beginPage = data.data.firstPage;//开始页
                $scope.endPage = data.data.lastPage;//结束页
                /*声明一个数组显示页数*/
                $scope.arrPage = new Array();
                $scope.fatherArrPage = new Array();

              if($scope.endPage>10){
                for(var a=0;a<$scope.endPage;a++){
                  $scope.fatherArrPage[a] = a + 1;
                }
                if(pageNum<9){
                  $scope.arrPage=$scope.fatherArrPage.splice(0,9)
                }else if(pageNum>=9){
                 $scope.arrPage=$scope.fatherArrPage.splice(pageNum-4,9)
               }
              }else{
                for (var j = 0; j < $scope.endPage; j++) {
                  $scope.arrPage[j] = j + 1;
                }
              }

                $scope.pageNow = pageNum;
                setTimeout(function () {
                    $('.select' + pageNum).css('color', 'red');
                }, 50);
                /**调用用户信息*/
                getusanme();
            } else {
                $(".pagCenter").hide();
                $('.caseBar_kong').show();
                $('.caseBar_list').hide();

                $scope.newXin_1Date = [];
            }

        }, function () {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
        });
    };

    /*分类应用列表点击改变页码*/
    $scope.changePage = function (currentPage) {

        if ($scope.pageSetting.pageNo == undefined) {
          $scope.pageSetting.pageNo = 1;
        }
        if ($scope.pageSetting.pageNo <= $scope.totalPage && $scope.pageSetting.pageNo > 0) {
            if ($scope.type == 7) {
                $scope.selectInfo(7, $scope.biglawuserid, $scope.pageSetting.pageNo, "10");
            } else {
                $scope.selectInfo(1, $scope.biglawuserid, $scope.pageSetting.pageNo, "10");
            }
        }

        else {
            console.log('列表页码越界!');
        }
    };

    //调取用户信息
    function getusanme() {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        caseBarService.SelectPersonSetInfo($scope.biglawuserid).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            if (data.code == 1) {
                $scope.mNewCase.DataUser = data.data;
            }
        }, function () {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
        });
    }

    /* 新建案件类别, 案件类型, 审理程序接口*/

    $scope.newXin = function () {
      // $("#myModal2").slideDown("slow");
      $('#myModal2').css('display','block');
      // $("#myModal21").css({'top':-($("#myModal21").height()+50)+'px'}).animate({top:'54px'});
      $("#myModal21").css({'top':-($(window).height()-54)+'px'}).animate({top:'54px'});
      // $('#myModal2').show();
      $rootScope.showCaseDiv = true;
      $scope.caseCateTypeButton(1);
      $scope.createCaseDiv = 1;
      $("#getSS label input").removeAttr("checked");
      $scope.showModule.addModule = false;
      $("#textcontent").css('display','none');
      //案由
      $scope.CaseAnyouModel = '';
      //身份
      $scope.mNewCase.caseclienttype = '';
      //委托人
      $scope.addPerList = [];
      caseBarService.getCaseCategory().then(function (data) {
        if (data.code == 1) {
          $scope.caseCategory = data.data;
         // $('.wtr').css("min-height", "20px");
        }
        $rootScope.loadingShow = false;
        $rootScope.fadeShow = false;
      }, function () {
        $rootScope.loadingShow = false;
        $rootScope.fadeShow = false;
      });


    };
    $scope.newXinMove=function(){
      $rootScope.newXinToggle=true;
    }
  $rootScope.closeModal = function(){
    // $('#myModal2').hide();
    $('#myModal2').css('display','none');
    $("#myModal21").animate({top:-($(window).height()-44)+'px'});

    $rootScope.showCaseDiv = false;
    setmodaldata();
    // $scope.selectInfo(1, $scope.biglawuserid, "1", "10");
  }

    /**新建案件的多有方法*/
    newCase.textmm($scope,$rootScope,$state,$cookies);

    //设置图片加载的路径 如果收藏 显示红星 没有收藏则是白色的星星
    function setImg(data) {
        data2 = data.list;

        for (var i = 0; i < data2.length; i++) {
            if (data2[i].isPrior == 1) {
              data2[i].imgUrl = "img/icon_Top2.svg";
              data2[i].flag=1;

            } else {
              data2[i].imgUrl = "img/icon_Top1.svg";
              data2[i].flag=0;
            }
            if(data2[i].teamid >0){
              if(data2[i].caseClassificationId==1){
                data2[i].teamC='团队办理';
                data2[i].imgUrlT = "img/ss-teamhandle.svg";
              }else if(data2[i].caseClassificationId==2){
                data2[i].teamC='团队办理';
                data2[i].imgUrlT = "img/fs-teamhandle.svg";
              }else if(data2[i].caseClassificationId==3){
                data2[i].teamC='团队办理';
                data2[i].imgUrlT = "img/fl-teamhandle.svg";
              }else if(data2[i].caseClassificationId==4){
                data2[i].teamC='团队办理';
                data2[i].imgUrlT = "img/xm-teamhandle.svg";
              }

            }else{
              if(data2[i].caseClassificationId==1){
                data2[i].imgUrlT = "img/ss-Singlehandle.svg";
                data2[i].teamC='个人办理';
              }else if(data2[i].caseClassificationId==2){
                data2[i].imgUrlT = "img/fs-Singlehandle.svg";
                data2[i].teamC='个人办理';
              }else if(data2[i].caseClassificationId==3){
                data2[i].imgUrlT = "img/fl-Singlehandle.svg";
                data2[i].teamC='个人办理';
              }else if(data2[i].caseClassificationId==4){
                data2[i].imgUrlT = "img/xm-Singlehandle.svg";
                data2[i].teamC='个人办理';
              }

            }
        }
        $scope.newXin_1Date = data2;
    }





    //跳转详情
    $scope.goCaseBarDetailxia = function (appId, appName, favoriteImgUrl) {
        $state.go("caseBarDetailxia", {type: $scope.type});


        $cookies.put('caseAppId', appId, {
            expires: expireDate
        });
        $cookies.put('caseAppName', appName, {
            expires: expireDate
        });
        $cookies.put('favoriteImgUrl', favoriteImgUrl, {
            expires: expireDate
        });
        $cookies.put('type', $scope.type, {//已归档和处理中类型
            expires: expireDate

        });
      $rootScope.typeIf=$scope.type;
      // sessionStorage.setItem('typeIf', $scope.type)

        // 案件TAB位置清空
        $cookies.put('caseDetailBarIndex', '');

    };

  $scope.showteamLayout = false;

  $scope.showTeamDiv = function(){
    if($('.showTeam').find('.test')){
      $scope.showteamLayout = true
    }
  }
  $scope.hideTeamDiv = function(){

      $scope.showteamLayout = false

  };
  //案件置顶

  $scope.favoriteT = function ($event,caseId,Flag) {
    var flag=!Flag;
    $rootScope.loadingShow = true;
    $rootScope.fadeShow = true;
    $scope.CaseId=caseId;
    $event.stopPropagation()
    var _this=$event.target;
    caseBarDetailBarService.setCaseUsed($scope.CaseId,flag).then(function (response) {
      $rootScope.loadingShow = false;
      $rootScope.fadeShow = false;
      if (response.code == 1) {
        if (response.data == 1) {
          EstablishNoticeStrip("img/icon_Success.svg",'案件置顶成功')
          /*设置五角星为黄色*/
          $(_this).attr("src", "img/icon_Top2.svg");
        }else {
          $(_this).attr("src", "img/icon_Top1.svg");
        }
        $scope.init()
      }
    }, function (response) {
      //失败
      $rootScope.loadingShow = false;
      $rootScope.fadeShow = false;
      EstablishNoticeStrip("img/icon_fail.svg",'案件置顶失败')
      console.error('favorite error', response.status, response.statusText);
    });

  };
//案件归档
  $scope.archiveConFirm = function ($event,CaseId) {
    /*$rootScope.loadingShow = true;
     $rootScope.fadeShow = true;*/
    $event.stopPropagation();
    var caseId=CaseId;
    bootbox.dialog({
      message: "归档案件后只可查看详情，不可进行其他操作。归档的案件可以撤销归档，并且数据不会丢失，您确定要归档该案件？",
      title: "归档案件",
      className:'d-promptBox',
      buttons: {
        OK: {
          label: "确定",
          className: "d-btn-confirm",

          callback: function () {
            caseBarDetailBarService.setCaseArchive(caseId).then(function (response) {
              $rootScope.loadingShow = false;
              $rootScope.fadeShow = false;
              /*请求成功*/
              if (response.code == 1) {
                $rootScope.typeIf=7;
                $state.go('caseBar', {from: 1});
                EstablishNoticeStrip("img/icon_Success.svg",'案件归档成功')
                if(response.data==null){
                  $('.caseBar_kong').addClass('.caseBar_list');
                }else {
                  $('.caseBar_kong').removeClass('.caseBar_list');
                }
                $scope.init();
              } else {
                bootbox.alert({
                  size: "small",
                  title: "提示信息",
                  message: response.message,
                  callback: function(){ /* your callback code */ }
                });
              }
            }, function (response) {
              $rootScope.loadingShow = false;
              $rootScope.fadeShow = false;
              EstablishNoticeStrip("img/icon_fail.svg",'案件归档失败')
              console.error('setCaseArchive error', response.status, response.statusText);
            });
          }
        },
        Cancel: {
          label: "取消",
          className: "d-btn-cancel",
          callback: function () {

          }
        }
      }
    });

  };

  //案件删除
  $scope.deleteConFirm = function ($event,CaseId) {
    $event.stopPropagation();
    var caseId=CaseId;
    bootbox.dialog({
      message: "删除案件后，所有与案件相关的信息将会被永久删除。这是一个不可逆操作，您确定要删除该案件？",
      title: "删除案件",
      className:'d-promptBox',
      buttons: {
        OK: {
          label: "确定",
          // className: "btn-default",
          className: "d-btn-confirm2",
          // confirmButtonColor: "#DD6B55",
          callback: function () {
            caseBarDetailBarService.deleteCase(caseId,loginName).then(function (response) {
              $rootScope.loadingShow = false;
              $rootScope.fadeShow = false;
              /*请求成功*/
              if (response.code == 1) {
                /*隐藏确认弹出框*/
                // $("#deleteModal").modal('hide');
                EstablishNoticeStrip("img/icon_Success.svg",'案件删除成功')
                $state.go('caseBar', {from: 1});
                $scope.init()
                $(this).removeClass('L_caseBaseIcon');
                $(this).removeClass('L_caseBaseP');
                // $scope.addRole.caseSetMore = false;
              }
              // else {
              //     bootbox.alert({
              //         size: "small",
              //         title: "提示信息",
              //         message: response.message,
              //         callback: function(){ /* your callback code */ }
              //     });
              //   $scope.addRole.caseSetMore == false;
              // }
            }, function (response) {
              $rootScope.loadingShow = false;
              $rootScope.fadeShow = false;
              EstablishNoticeStrip("img/icon_fail.svg",'案件删除失败')
              console.error('deleteConfirm error', response.status, response.statusText);
            });
          }
        },
        Cancel: {
          label: "取消",
          // className: "btn-default",
          className: "d-btn-cancel",
          // confirmButtonColor: "#DD6B55",
          callback: function () {
          }
        }
      }
    });
    // $rootScope.loadingShow = true;
    // $rootScope.fadeShow = true;
  };

  //取消归档
  $scope.archiveCanCel = function ($event,CaseId) {
    event.stopPropagation()
    var caseId=CaseId;
    bootbox.dialog({
      message: "撤销归档后，可以对该案件进行所有操作。您确定要撤销该案件归档？",
      title: "撤销归档",
      className:'d-promptBox',
      buttons: {
        OK: {
          label: "确定",
          className: "d-btn-confirm",

          callback: function () {
            caseBarDetailBarService.setCaseUnArchive(caseId).then(function (response) {
              $rootScope.loadingShow = false;
              $rootScope.fadeShow = false;
              /*请求成功*/
              if (response.code == 1) {
                /*隐藏确认弹出框*/
                // $("#archiveModal").modal('hide');
                /*跳转到已归档案件列表*/
                $state.go('caseBar', {from: 7});
                $rootScope.typeIf=1;
                EstablishNoticeStrip("img/icon_Success.svg",'案件撤销归档成功')
                $scope.init();
                if(response.data==null){
                  $('.caseBar_kong').addClass('.caseBar_list');
                }else {
                  $('.caseBar_kong').removeClass('.caseBar_list');
                }
              } else {
                bootbox.alert({
                  size: "small",
                  title: "提示信息",
                  message: response.message,
                  callback: function(){ /* your callback code */ }
                });
              }
            }, function (response) {
              $rootScope.loadingShow = false;
              $rootScope.fadeShow = false;
              EstablishNoticeStrip("img/icon_fail.svg",'案件撤销归档失败')
              console.error('setCaseArchive error', response.status, response.statusText);
            });
          }
        },
        Cancel: {
          label: "取消",
          className: "d-btn-cancel",
          callback: function () {

          }
        }
      }
    });

    /* /!*隐藏确认弹出框*!/
     $("#archiveModal").modal('hide');
     $('.modal-backdrop').hide();*/
  };
  // 创建提示
  function EstablishNoticeStrip (Url,Content){
    var EstablishNotice=document.createElement('div');
    EstablishNotice.style.cssText="width:276px;height:46px;background:#fff;margin-left:276px;overflow: hidden;z-index:99999";
    EstablishNotice.innerHTML="<p style='width:11px;height:46px;background-image: linear-gradient(-180deg, #02BABC 0%, #0D9DE4 100%);float:left;'></p>" +
     "<div style='width:200px;height:46px;float:left;vertical-align:middle;padding:10px 0;box-sizing:border-box;text-indent: 35px;'>"+"<img src="+Url+" style='margin-right:32px'>"+Content+"</div>" +
      "<span  class='icon iconfont' style='font-size: 16px;cursor:pointer;float:right;margin-right:30px;margin-top:10px; '>&times;</span>";
    var _this=$(EstablishNotice);
    $scope.time=$timeout(function(){
      _this.addClass('EstablishNotic');
      $scope.timeE=$timeout(function(){
        _this.removeClass('EstablishNotic').addClass('EstablishNoticB') ;
        $scope.timeR=$timeout(function(){
          _this.remove();
        },1000);
      },3000)
    },200);
    $('#casebar-header-content').prepend(EstablishNotice);
    $('#casebar-header-content span').bind('click',function(){
      $scope.timeEr=$timeout(function(){
        _this.removeClass('EstablishNotic').addClass('EstablishNoticB') ;
        $scope.timeRr=$timeout(function(){
          _this.remove();
        },1000);
      },300)
    });
  };

  /*初始化方法*/
  $scope.init();

};
